/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.facade.util;

import com.googlecode.bambole.domain.OrganizationNote;
import com.googlecode.bambole.util.SendEmailMessageUtil;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Stateless
public class SchedulerUtil {

    @EJB
    private com.googlecode.bambole.facade.OrganizationNoteFacade organizationFacade;

    @Schedule(minute = "*", second = "0", dayOfMonth = "*", month = "*", year = "*", hour = "*", dayOfWeek = "Mon-Fri", persistent = false)

    public void sendEmailMessage() {

        Logger.getAnonymousLogger().log(Level.INFO, "Send email message: {0}", new Date());

        List<OrganizationNote> organizationNoteList = organizationFacade.findNotesToSendEmailMessage();

        if (organizationNoteList != null && !organizationNoteList.isEmpty()) {

            Logger.getAnonymousLogger().log(Level.INFO, "Send email message - organization notes: {0}", new Date());

            for (OrganizationNote organizationNote : organizationFacade.findNotesToSendEmailMessage()) {

                String recipient = organizationNote.getUpdatedBy().getEmailAddress();

                String subject = ResourceBundle.getBundle("/bundle", Locale.forLanguageTag(organizationNote.getUpdatedBy().getLocale())).getString("Bambole") + " - "
                        + ResourceBundle.getBundle("/bundle", Locale.forLanguageTag(organizationNote.getUpdatedBy().getLocale())).getString("Note") + " - "
                        + ResourceBundle.getBundle("/bundle", Locale.forLanguageTag(organizationNote.getUpdatedBy().getLocale())).getString("Organization") + " : "
                        + organizationNote.getOrganization().getName();

                String message = organizationNote.getDescription();

                if (SendEmailMessageUtil.sendEmailMessage(recipient, subject, message)) {
                    organizationNote.setEmailSent(true);
                    organizationFacade.edit(organizationNote);
                    Logger.getAnonymousLogger().log(Level.INFO, "Send email message - sent - organization notes: {0}", organizationNote.getId());
                } else {
                    Logger.getAnonymousLogger().log(Level.WARNING, "Send email message - fail - organization notes: {0}", organizationNote.getId());
                }
            }
        }
    }
}
